import { getUserInfo } from '@/AGC/utils/user'
import { useUserStore } from '@/stores/user'
import { clearInfo } from '@/utils'
import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/LoginPage.vue'),
      meta: {
        hidden: true,
      },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/login/RegisterPage.vue'),
      meta: {
        hidden: true,
      },
    },
    {
      path: '/modify',
      name: 'modify',
      component: () => import('@/views/login/ModifyPage.vue'),
      meta: {
        hidden: true,
      },
    },
    {
      path: '/',
      redirect: '/plancustom',
      meta: {
        hidden: true,
      },
    },
    {
      path: '/plancustom',
      name: 'plancustom',
      meta: {
        keepAlive: true,
      },
      component: () => import('@/layouts/MainLayout.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/plan_custom/PlanCustom.vue'),
          name: 'plancustomChild',
          meta: {
            title: '智能推荐',
            keepAlive: true,
          },
        },
      ],
    },
    {
      path: '/food',
      name: 'food',
      component: () => import('@/layouts/MainLayout.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/food/FoodPage.vue'),
          meta: {
            title: '食物查询',
          },
        },
      ],
    },
    {
      path: '/exercies',
      name: 'exercise',
      component: () => import('@/layouts/MainLayout.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/exercise/ExercisePage.vue'),
          meta: {
            title: '运动查询',
          },
        },
      ],
    },
    {
      path: '/record',
      name: 'record',
      component: () => import('@/layouts/MainLayout.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/record/RecordPage.vue'),
          meta: {
            title: '数据记录',
          },
        },
      ],
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('@/layouts/MainLayout.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/user-center/UserCenter.vue'),
          meta: {
            title: '个人中心',
          },
        },
      ],
    },
  ],
})

// 白名单
const whiteList = ['/login', '/register', '/modify']

// 导航守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  if (userStore.token) {
    if (whiteList.includes(to.path)) {
      next('/')
    } else {
      if (!userStore.userInfo.id) {
        // 1、获取用户信息
        const res = await getUserInfo(userStore.token)

        // 2、设置用户信息
        if (res) {
          userStore.setUserInfo(res)
        } else {
          ElMessage.error('token失效, 请重新登录')
          await clearInfo()
          next('/login')
          return
        }
      }

      next()
    }
  } else {
    await clearInfo()
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {})
export default router
